import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home.vue'
// 设定一个场景 进入一个动物园 动物园的猴子 斑马 可以不用买票直接观看
// 熊猫 老虎需要买票才能进行观看 
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: 'monkey',
        name: "monkey",
        component: () => import ('../views/monkey')
      },
      {
        path: 'bm',
        name: "bm",
        component: () => import ('../views/bm')
      },
      {
        path: 'tiger',
        name: "tiger",
        meta: {
          // 设置用户是否必须买票
          aaa: true
        },
        component: () => import ('../views/tiger')
      },
      {
        path: 'panda',
        name: "panda",
        // meta中是路由元信息 它里面的键值对是可以自定义的
        meta: {
          aaa: true
        },
        component: () => import ('../views/panda')
      },
      {
        path: 'ticket',
        component: () => import ('../views/ticket')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to)
  // if(to.name == "tiger" || to.name == "panda") {
    // if(JSON.parse(localStorage.getItem('ticket'))) {
    //   next()
    // } else {
    //   alert('你还没有买票')
    //   next('/ticket')
    // }
  // } else {
  //   next()
  // }
  if(to.matched.some(item => item.meta.aaa)) {
    if(JSON.parse(localStorage.getItem('ticket'))) {
      next()
    } else {
      alert('你还没有买票')
      next('/ticket')
    }
  } else {
    next()
  }
  
})
export default router
